﻿<!--#include file="../Include/Config.asp"-->
<!--#include file="../Template.asp"-->
<%
'----------------------------------------------------------
'页面功能：问卷显示和保存页，本站核心文件。
'最后修改时间：2007-11-23
'作者：legal http://www.ilegal.cn
'备注：
'----------------------------------------------------------
'显示网页keywords和description
call Check_Site_State()
'----------------------------------------------------------
'读取问卷常用变量---start
'----------------------------------------------------------
dim Survey_id,Action,Survey_content,i,Question_type
dim Survey_question,Question_answer,j
Question_type=""
Survey_id=request.QueryString("Survey_id")
Action=request.QueryString("Action")
call CheckNum(Survey_id)
sql="select * from survey where Survey_id="&Survey_id
rs.open sql,conn,1,3
if rs.eof then
	call ShowAlert("参数错误","")
else
	Survey_content=rs("Survey_content")
	Survey_name=rs("Survey_name")
	Survey_description=rs("Survey_description")
	Survey_ipRepeat=rs("Survey_ipRepeat")
	Survey_jdtz=rs("Survey_jdtz")
	Survey_back=rs("Survey_back")
	Survey_needUserInfo=rs("Survey_needUserInfo")
	Survey_template=rs("Survey_templet")
end if
rs.close
'----------------------------------------------------------
'读取问卷常用变量---over
'----------------------------------------------------------


'----------------------------------------------------------
'检验问卷开放、审核、密码、IP限制等---start
'----------------------------------------------------------
dim surveyQuestionList:surveyQuestionList=""
dim canShow:canShow=1'是否可以显示问卷的标示
'----------------------------------------------------------
'检验问卷（是否审核、是否为私有问卷、是否设置密码、是否在ip范围内）over
'----------------------------------------------------------

if canShow=1 then'假如检验问卷通过，则显示问卷内容，如果不通过，则显示密码输入框（提示错误）
'----------------------------------------------------------
'显示问卷，将结果赋值给surveyQuestionList，模板操作---over
'----------------------------------------------------------
'无截断跳转的问卷按一次性全部显示
	surveyQuestionList=surveyQuestionList & "<div class=""center"">"
	surveyQuestionList=surveyQuestionList & "<form id=""form_survey"" name=""form1"" action=""?Survey_id="&Survey_id&"&Action=survey_save"" method=""post"" onsubmit=""survey_submit();"">"
	surveyQuestionList=surveyQuestionList & "<div class=""name"">"
	surveyQuestionList=surveyQuestionList & Survey_name
	surveyQuestionList=surveyQuestionList & "</div>"
	surveyQuestionList=surveyQuestionList & "<div class=""Question_list"">"
	if Survey_needUserInfo=1 then'假如问卷要求输入用户信息，则显示用户信息
	surveyQuestionList=surveyQuestionList & "姓名：<input name=""user_name"">&nbsp;&nbsp;"
	surveyQuestionList=surveyQuestionList & "电子邮件：<input name=""user_email"">"
	end if
	if Survey_content<>"" then
	Survey_question=split(Survey_content,",")
	surveyQuestionList=surveyQuestionList & "<ul type=""1"">"
	for i=0 to ubound(Survey_question)
	sql="select * from question where Question_id="&Survey_question(i)
	rs.open sql,conn,1,1
	if not rs.eof then
	
	select case rs("Question_type")
	case "1"
	 surveyQuestionList=surveyQuestionList & "<li>"&rs("Question_content")&"</li>"
	 surveyQuestionList=surveyQuestionList & "<ul type=""1"">"
	 if not rs("Question_answer")="" then
		if Question_type<>"" then'将题目类型存在变量中，统一POST
		   Question_type=Question_type&","&rs("Question_type")
		else 
		   Question_type=rs("Question_type")
		end if
	 Question_answer=split(rs("Question_answer"),"&")
	 Question_img=split(rs("Question_img"),"&")
	 for j=0 to ubound(Question_answer)
	 surveyQuestionList=surveyQuestionList & "<li><input type=""radio"" name=""answer"&Survey_question(i)&""" value="""&j&""">"&Question_answer(j)&"</li>"
	 if Question_img(j)<>"null" then
	 surveyQuestionList=surveyQuestionList & "<img src=""../"&Question_img(j)&""">"
	 end if
	 next
	 else
	 surveyQuestionList=surveyQuestionList & "该题目下无选项"'出现此情况，刚可以认为问卷无效，一定是出现异常。
	 end if
	 surveyQuestionList=surveyQuestionList & "</ul><br/>"
	 
	case "2"
	 surveyQuestionList=surveyQuestionList & "<li>"&rs("Question_content")&"</li>"
	 surveyQuestionList=surveyQuestionList & "<ul type=""1"">"
	 if not rs("Question_answer")="" then
		if Question_type<>"" then  '将题目类型存在变量中，统一POST
		   Question_type=Question_type&","&rs("Question_type")
		else 
		   Question_type=rs("Question_type")
		end if
	 Question_answer=split(rs("Question_answer"),"&")
	 Question_img=split(rs("Question_img"),"&")
	 for j=0 to ubound(Question_answer)
	 surveyQuestionList=surveyQuestionList & "<li><input type=""checkbox"" name=""answer"&Survey_question(i)&""" value="""&j&""">"&Question_answer(j)&"</li>"
	 if Question_img(j)<>"null" then
	 surveyQuestionList=surveyQuestionList & "<img src=""../"&Question_img(j)&""">"
	 end if
	 next
	 else
	 surveyQuestionList=surveyQuestionList & "该题目下无选项"'出现此情况，刚可以认为问卷无效，一定是出现异常。
	 end if
	 surveyQuestionList=surveyQuestionList & "</ul><br/>"
	
	case "3"
	 surveyQuestionList=surveyQuestionList & "<li>"&rs("Question_content")&"</li>"
	 surveyQuestionList=surveyQuestionList & "<ul type=""1"">"
	 if not rs("Question_answer")="" then
		if Question_type<>"" then'将题目类型存在变量中，统一POST
		   Question_type=Question_type&","&rs("Question_type")
		else 
		   Question_type=rs("Question_type")
		end if
	 Question_answer=split(rs("Question_answer"),"&")
	 Question_img=split(rs("Question_img"),"&")
	 for j=0 to (ubound(Question_answer)-1)
	 surveyQuestionList=surveyQuestionList & "<li><input type=""radio"" name=""answer"&Survey_question(i)&""" value="""&j&""">"&Question_answer(j)&"</li>"
	 if Question_img(j)<>"null" then
	 surveyQuestionList=surveyQuestionList & "<img src=""../"&Question_img(j)&""">"
	 end if
	 next'输出最后一个文本类型的
	 surveyQuestionList=surveyQuestionList & "<li><input type=""radio"" name=""answer"&Survey_question(i)&""" value="""&ubound(Question_answer)&""">"&Question_answer(ubound(Question_answer))&"<input type=""text"" name=""answer"&Survey_question(i)&"_2""></li>"
	 else
	 surveyQuestionList=surveyQuestionList & "该题目下无选项"'出现此情况，刚可以认为问卷无效，一定是出现异常。
	 end if
	 surveyQuestionList=surveyQuestionList & "</ul><br/>"
	
	case "4"
	 surveyQuestionList=surveyQuestionList & "<li>"&rs("Question_content")&"</li>"
	 surveyQuestionList=surveyQuestionList & "<ul type=""1"">"
	 if not rs("Question_answer")="" then
		if Question_type<>"" then  '将题目类型存在变量中，统一POST
		   Question_type=Question_type&","&rs("Question_type")
		else 
		   Question_type=rs("Question_type")
		end if
	 Question_answer=split(rs("Question_answer"),"&")
	 Question_img=split(rs("Question_img"),"&")
	 for j=0 to (ubound(Question_answer)-1)
	 surveyQuestionList=surveyQuestionList & "<li><input type=""checkbox"" name=""answer"&Survey_question(i)&""" value="""&j&""">"&Question_answer(j)&"</li>"
	 if Question_img(j)<>"null" then
	 surveyQuestionList=surveyQuestionList & "<img src=""../"&Question_img(j)&""">"
	 end if
	 next'输出最后一个文本类型的
	 surveyQuestionList=surveyQuestionList & "<li><input type=""checkbox"" name=""answer"&Survey_question(i)&""" value="""&ubound(Question_answer)&""">"&Question_answer(ubound(Question_answer))&"<input type=""text"" name=""answer"&Survey_question(i)&"_2""></li>"
	
	 else
	 surveyQuestionList=surveyQuestionList & "该题目下无选项"'出现此情况，刚可以认为问卷无效，一定是出现异常。
	 end if
	 surveyQuestionList=surveyQuestionList & "</ul><br/>"
	
	case "5"
	surveyQuestionList=surveyQuestionList & "<li>"&rs("Question_content")&"</li><br>"
	surveyQuestionList=surveyQuestionList & "回答："
	surveyQuestionList=surveyQuestionList & "<input type=""text"" name=""answer"&Survey_question(i)&"""><br/><br/>"
		if Question_type<>"" then  '将题目类型存在变量中，统一POST
		   Question_type=Question_type&","&rs("Question_type")
		else 
		   Question_type=rs("Question_type")
		end if
	case else 
	end select
	
	
	end if
	rs.close
	next
	surveyQuestionList=surveyQuestionList & "</ul>"
	surveyQuestionList=surveyQuestionList & "<input type=""hidden"" value="""&Question_type&""" name=""Question_type"">"
	surveyQuestionList=surveyQuestionList & "</div>"'end of question_list
	surveyQuestionList=surveyQuestionList & "<div class=""button"">"
	surveyQuestionList=surveyQuestionList & "</div>"
	surveyQuestionList=surveyQuestionList & "<div class=""backindex"">"
'	surveyQuestionList=surveyQuestionList & "<a href=""Index.asp"" target=""_self"">&nbsp;返回首页</a>"
	surveyQuestionList=surveyQuestionList & "</div>"
	
	surveyQuestionList=surveyQuestionList & "</form>"
	surveyQuestionList=surveyQuestionList & "</div>"'end of center
	conn.execute("update survey set Survey_hits=Survey_hits+1 where Survey_id="&Survey_id)
	else
	surveyQuestionList=surveyQuestionList & "本问卷下暂时没有问题，请等待管理员添加"
	end if
	'---------------------------------------------------

'----------------------------------------------------------
'显示问卷，将结果赋值给surveyQuestionList，模板操作---over
'----------------------------------------------------------
end if



'----------------------------------------------------------
'模板操作开始
'----------------------------------------------------------
if Survey_template<>0 then
sql="select * from templet where Templet_column=9 and Templet_id=" & Survey_template
else
sql="select * from templet where Templet_default=1 and Templet_column=9"
end if 
rs.open sql,conn,1,1
if rs.eof then
rs.close
sql="select * from templet where Templet_default=1 and Templet_column=9"
rs.open sql,conn,1,1
end if
if rs.eof then
Call ShowAlert("模板读取错误，请联系网站管理员","")
end if
Templet_content=rs("Templet_content")
rs.close
while cint(InStr(Templet_content,"{$Templet("))>0
	Templet_str=mid(Templet_content,cint(InStr(Templet_content,"{$Templet("))+2,cint(InStr(Templet_content,")Templet$}"))-cint(InStr(Templet_content,"{$Templet("))-2)    
	Templet_content=replace(Templet_content,"{$"&Templet_str&")Templet$}",Templet_change(replace(Templet_str,"Templet(","")))
wend
Templet_content=replace(Templet_content,"{$keywords$}",keywords)
Templet_content=replace(Templet_content,"{$descrip$}",descrip)
Templet_content=replace(Templet_content,"{$siteName$}",siteName)
Templet_content=replace(Templet_content,"{$Survey_name$}",Survey_name)
Templet_content=replace(Templet_content,"{$surveyQuestionList$}",surveyQuestionList)
while cint(InStr(Templet_content,"{$"))>0
Templet_content_func_all=mid(Templet_content,instr(Templet_content,"{$")+2,instr(Templet_content,"$}")-instr(Templet_content,"{$")-2)
Templet_content_func=split(Templet_content_func_all,"(")
Templet_content_func(1)=replace(Templet_content_func(1),")","")
Templet_content_para=split(Templet_content_func(1),",")
Select Case Templet_content_func(0)
	Case "FlashRing"
	    Templet_content=replace(Templet_content,"{$"&Templet_content_func_all&"$}",FlashRing(Templet_content_para(0),Templet_content_para(1),Templet_content_para(2),Templet_content_para(3)))
Case "Get_Survey_Paihang"
	    Templet_content=replace(Templet_content,"{$"&Templet_content_func_all&"$}",Get_Survey_Paihang(Templet_content_para(0),Templet_content_para(1),Templet_content_para(2),Templet_content_para(3)))
	Case "FriendLink"	
	    Templet_content=replace(Templet_content,"{$"&Templet_content_func_all&"$}",FriendLink(Templet_content_para(0),Templet_content_para(1),Templet_content_para(2),Templet_content_para(3),Templet_content_para(4)))
	Case "New_Survey_List"
	    Templet_content=replace(Templet_content,"{$"&Templet_content_func_all&"$}",New_Survey_List(Templet_content_para(0),Templet_content_para(1),Templet_content_para(2),Templet_content_para(3)))
	Case else
		Templet_content=replace(Templet_content,"{$"&Templet_content_func_all&"$}","")
End Select

wend
I1 Templet_content
'----------------------------------------------------------
'模板操作结束
'----------------------------------------------------------


select case Action
    case "survey_save" call survey_save
    case "sub_check_pass" call sub_check_pass
end select
'----------------------------------------------------------
'验证用户提交的密码---start
'----------------------------------------------------------
sub sub_check_pass
dim survey_pass:survey_pass=md5(request.Form("pass"),16)
sql="select * from survey where Survey_id="&Survey_id
rs.open sql,conn,1,3
if rs.eof then
call ShowAlert("参数错误","")
else
	if rs("Survey_password")=survey_pass then
		session("pass")=survey_pass
		Call ShowAlert("密码正确","Survey_Show.asp?Survey_id="&Survey_id&"")
	else
		Call ShowAlert("密码错误","Index.asp")
	end if
end if
rs.close
end sub
'----------------------------------------------------------
'验证用户提交的密码---over
'----------------------------------------------------------

'----------------------------------------------------------
'普通情况下的保存问卷数据过程---start
'----------------------------------------------------------
sub survey_save
I1 "split_||||"'分离页面信息和返回信息用
Survey_id=request.QueryString("Survey_id")
userDatum_email=trim(request.form("user_email"))
canSubmit=1
sql="select * from survey where Survey_id=" & Survey_id
rs.open sql,conn,1,3
if rs.eof then
	I1 "||||问卷ID错误"
	canSubmit=0
else
Survey_content=rs("Survey_content")
Survey_needUserInfo=rs("Survey_needUserInfo")
Survey_annal=rs("Survey_annal")
end if
rs.close
if Survey_ipRepeat="0" then'对于同一IP不可重复提交的问卷，进行IP验证
	UserIp=request.servervariables("http_x_forwarded_for") 
	if UserIp="" then 
		UserIp=request.servervariables("remote_addr")
	end if
	sql="select * from ip where Ip_survey="&Survey_id&" and Ip_content='" & UserIp & "'"
	rs.open sql,conn,1,3
	if not rs.eof then
		I1 "||||对不起，您已经提交过一份问卷！"
		canSubmit=0
	end if
	rs.close
end if
if Survey_needUserInfo=1 then'对于需要用户填写资料的问卷,根据email这一唯一标示进行用户是否已经提交的验证
userDatum_email=trim(request.Form("user_email"))
if userDatum_email="" then
	I1 "||||用户资料输入不完整或有误"
	canSubmit=0
else
	sql="select * from userdatum where userDatum_email='" & userDatum_email & "' and userDatum_belong=" & Survey_id
	rs.open sql,conn,1,3
		if not rs.eof then
			I1 "||||您已经提交过一份问卷"
			canSubmit=0
		end if
	rs.close
end if
end if
dim Question_result_new
Question_type_save=request.form("Question_type")
Survey_question_save=split(Survey_content,",")
Question_type_save=split(Question_type_save,",")
if ubound(Question_type_save)<>ubound(Survey_question_save) then
I1 "||||问卷错误,可能您一次打开多个问卷"
end if
for k=0 to ubound(Survey_question_save)'外循环，所有的题目扫描一遍,检验问卷填写是否完整
	value_d=request.Form("answer"&Survey_question_save(k))
	if value_d="" then
        I1 "||||第"&k+1&"道题目未填写完整"
		canSubmit=0
	end if
next
if canSubmit=0 then
response.End()
end if
application.Lock()
for k=0 to ubound(Survey_question_save)'外循环，所有的题目扫描一遍,保存结果到题目表中
select case Question_type_save(k)
  case "1","2"' 单项和多项选择题的数据保存
	value_d=request.Form("answer"&Survey_question_save(k))
	sql="select Question_result from question where Question_id="&Survey_question_save(k)
	rs.open sql,conn,1,3
	dim Question_result
	Question_result=split(rs("Question_result"),",")
	if len(value_d)>1 then'对于返回是数组的返回值，是对多项选择的处理
	value_d=split(value_d,",")
	for each val in value_d
	Question_result(val)=Question_result(val)+1
	next
	else
	Question_result(value_d)=Question_result(value_d)+1
	end if
	for each Qc in Question_result'新结果重新连接成一串数据
	if Question_result_new="" then
	Question_result_new=Qc
	else 
	Question_result_new=Question_result_new&","&Qc
	end if
	next
	rs("Question_result")=Question_result_new
	rs.update
	Question_result_new=""
	rs.close
  case "3","4"'单项、多项选择题和文本混合的数据保存
	value_d=request.Form("answer"&Survey_question_save(k))
	sql="select Question_result from question where Question_id="&Survey_question_save(k)
	rs.open sql,conn,1,3
	Question_result=split(rs("Question_result"),",")
	if len(value_d)>1 then'对于返回是数组的返回值，是对多项选择的处理
	value_d=split(value_d,",")
	for each val in value_d
		if cint(val)=cint(ubound(Question_result)) then'用户选择了多项选择题和文本混合题型的最后一项
			sqlt="select * from text"
			rst.open sqlt,conn,1,3
			rst.addnew
			rst("Text_content")=request.Form("answer"&Survey_question_save(k)&"_2")
			rst("Text_belong")=Survey_question_save(k)
			rst.update
			rst.close
		end if
	Question_result(val)=Question_result(val)+1
	next
	else
		if cint(value_d)=cint(ubound(Question_result)) then'用户选择了单项选择题和文本混合题型的最后一项
			sqlt="select * from text"
			rst.open sqlt,conn,1,3
			rst.addnew
			rst("Text_content")=request.Form("answer"&Survey_question_save(k)&"_2")
			rst("Text_belong")=Survey_question_save(k)
			rst.update
			rst.close
		end if
	Question_result(value_d)=Question_result(value_d)+1
	end if
	for each Qc in Question_result'新结果重新连接成一串数据
	if Question_result_new="" then
	Question_result_new=Qc
	else 
	Question_result_new=Question_result_new&","&Qc
	end if
	next
	rs("Question_result")=Question_result_new
	rs.update
	Question_result_new=""
	rs.close
  case "5"'纯文本的题目的数据保存
	value_d=request.Form("answer"&Survey_question_save(k))
	sql="select * from text"
	rs.open sql,conn,1,3
	rs.addnew
	rs("Text_content")=value_d
	rs("Text_belong")=Survey_question_save(k)
	rs.update
	rs.close
end select
next'扫描全部题目结束
if Survey_needUserInfo=1 then'对于需要用户填写资料的问卷的处理
    '对于需要记录单个用户结果的问卷，此处将所有的结果连接成字符串
	dim userDatum_result:userDatum_result=""
	dim userDatum_result_arr:userDatum_result_arr=""
	Dim value_d
	if Survey_annal=1 then
		for k=0 to ubound(Survey_question_save)'外循环，所有的题目扫描一遍,保存结果
		    userDatum_result=""
			select case Question_type_save(k)
				case "1","2"' 单项和多项选择题的数据保存
				    value_d=request.Form("answer"&Survey_question_save(k))
					value_d=split(value_d,",")
					for each val in value_d
						if userDatum_result<>"" then
							userDatum_result=userDatum_result&"$$$$"&val
						else
							userDatum_result=val
						end if
					next
				case "3","4"
				sql="select Question_answer from question where Question_id="&Survey_question_save(k)
				rs.open sql,conn,1,3
				dim Question_answer
				Question_answer=split(rs("Question_answer"),"&")
				rs.close
				value_d=request.Form("answer"&Survey_question_save(k))
				value_d=split(value_d,",")
				for i=0 to ubound(value_d)
					if  ubound(Question_answer)<>value_d(i) then
						if userDatum_result<>"" then
							userDatum_result=userDatum_result&"$$$$"&value_d(i)&"::::"&value_d(i)
						else
							userDatum_result=value_d(i)&"::::"&value_d(i)
						end if
					else
						text_temp=replace(request.Form("answer"&Survey_question_save(k)&"_2"),"::::","")
						if userDatum_result<>"" then
							userDatum_result=userDatum_result&"$$$$"&value_d(i)&"::::"&text_temp
						else
							userDatum_result=value_d(i)&"::::"&text_temp
						end if
					end if
				next
				case "5"
				userDatum_result=trim(request.Form("answer" & Survey_question_save(k)))
	        end select
			if userDatum_result_arr<>"" then
			'将所有的题目的结果连接成字符串
			userDatum_result_arr=userDatum_result_arr&"||||"&userDatum_result
			else
			userDatum_result_arr=userDatum_result
			end if
		next
	end if
	sql="select * from userdatum"
	rs.open sql,conn,1,3
	rs.addnew
	rs("userDatum_name")=trim(request.Form("user_name"))
	rs("userDatum_email")=userDatum_email
	rs("userDatum_belong")=Survey_id
	rs("userDatum_result")=userDatum_result_arr
	rs("userDatum_date")=now()
	rs.update
	rs.close
end if

'有效问卷加1
sql="select Survey_usehits from survey where Survey_id="&Survey_id
rs.open sql,conn,1,3
rs(0)=rs(0)+1
rs.update
rs.close
if Survey_ipRepeat="0" then'对于同一IP不可重复提交的问卷，记录IP
	UserIp=request.servervariables("http_x_forwarded_for") 
	if UserIp="" then 
	UserIp=request.servervariables("remote_addr")
	end if
	sql="select * from ip"
	rs.open sql,conn,1,3
	rs.addnew
	rs("Ip_content")=UserIp
	rs("Ip_survey")=Survey_id
	rs("Ip_date")=date()
	rs.update 
	rs.close
end if
application.unLock()
session("pass")=""
I1 "问卷提交成功，感谢您的参与!!"
I1 "split_||||"'分离页面信息和返回信息用
end sub
'----------------------------------------------------------
'普通情况下的保存问卷数据过程---over
'----------------------------------------------------------
%>
<script language="javascript">
function survey_submit(){
    var options = {
		beforeSubmit:   showRequest_survey_submit,
		success:        showResponse_survey_submit
	};
    $('#form_survey').ajaxForm(options);
   
}
function showRequest_survey_submit(formData,jqForm,options){
	var queryString = $.param(formData);
	return true;
}
function showResponse_survey_submit(responseText,statusText){
	var text=responseText.split('split_||||');
	if (text[1].search("成功")>0){
	$('#form_survey').clearForm();
	alert(text[1]);
	location.href='Index.asp';
		 }else
	alert('由于以下原因，问卷不能正常提交：\n'+text[1].replace(/\|\|\|\|/gi,"\n"));
}
</script>